import axios from "../../utils/request"
import {loginPost,regPost} from "../../api/login"
export default {
    namespaced:true,
    state: {
        users:JSON.parse(localStorage.getItem('users'))||{}
    },
    mutations: {
        GET_LOGIN(state,payload){
            state.users={...payload}
            localStorage.setItem('token',payload.token)
            localStorage.setItem('users',JSON.stringify(payload))
        }
    },
    actions: {
        getLogin({commit},payload){
            return new Promise(async(resolve,reject)=>{
                const data =await loginPost('/login',payload)
                if(data.status===200&&data.data.code===200){
                    resolve(data.data.msg)
                    commit('GET_LOGIN',data.data)
                }else{
                    reject(data.data.msg)
                }
            })
        },
        getReg({commit},payload){
            console.log(payload)
            return new Promise(async(resolve,reject)=>{
                const data=await regPost('/register',payload)

                if(data.status===200&&data.data.code===200){
                    resolve(data.data.msg)
                }else{
                    reject(data.data.msg)
                }
            })
        }
    }
}